iT邦幫忙

2022 iThome 鐵人賽

DAY 27
0
自我挑戰組

30天HackerRank 1 Month Preparation Kit系列 第 27

Day 27 New Year Chaos用Golang

  • 分享至 

  • xImage
  •  

題目

https://ithelp.ithome.com.tw/upload/images/20220927/20151833w2Pg0adii3.png
https://ithelp.ithome.com.tw/upload/images/20220927/20151833xrgYCxhnWm.png
https://ithelp.ithome.com.tw/upload/images/20220927/20151833Jh3bNT1fXW.png

解題想法

func minimumBribes(q []int32) {
    // Write your code here
    bribes := 0
    
    for i:=len(q)-1; i >= 0; i-- {
        diff := int(q[i]) - (i+1)
        if  diff > 2 {
            fmt.Println("Too chaotic")
            return
        }
        for j:=int(math.Max(float64(0), float64(q[i]-2))); j < i; j++ {
            if q[j] > q[i] {
                bribes++
            }
        }
        
    }
    
    fmt.Println(bribes) 
    return
}

結果

https://ithelp.ithome.com.tw/upload/images/20220927/20151833BTuYdtNqFZ.png
https://ithelp.ithome.com.tw/upload/images/20220927/2015183387tWcTCalC.png


上一篇
Day26 Reverse a linked list用Golang
下一篇
Day 28 Insert a node at a specific position in a linked list用Golang
系列文
30天HackerRank 1 Month Preparation Kit30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言